package pos.utils;

import com.sun.j3d.utils.geometry.Text2D;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.*;
import javax.media.j3d.Appearance;
import javax.media.j3d.Node;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;

/**
 *
 * @author plus
 */
public class TabBar2D extends TransformGroup implements IContainer, Pickable, Invokeable{
    private final static int STATUE_INIT=1, STATUE_FIXED=2;
    private int currentStatue=STATUE_FIXED;
    private double progress=1;
    private static ArrayList<ImageButton2D> subTabs;
    private static HashMap<Integer,TransformGroup> subTabTG;
    private static HashMap<Integer,ArrayList<Pickable>> subItems;
    private Pickable currentItem,currentTab;
    private int currentTabID;
    private Transform3D myTransform;
    private IContainer myHost;
    private Vector3d pos;
    
    /**
     */
    public TabBar2D(Vector3d pos) {
        this.pos = pos;
        myTransform = new Transform3D();
        myTransform.set(pos);
        this.setTransform(myTransform);
        this.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        subTabs=new ArrayList<ImageButton2D>();
        subTabTG=new HashMap<Integer,TransformGroup>();
        subItems=new HashMap<Integer,ArrayList<Pickable>> ();
    }

    public int addSubTab(ImageButton2D tab){
        tab.setHost(this);
        tab.setEventName("ChangeTab");
        this.addChild(tab);
        
        subTabs.add(tab);
        currentItem=subTabs.get(0);
        currentTabID=subTabTG.size();
        
        TransformGroup TGTab=new TransformGroup();
        TGTab.setCapability(ALLOW_TRANSFORM_WRITE);
        this.addChild(TGTab);
        subTabTG.put(currentTabID, TGTab);
        subItems.put(currentTabID, new ArrayList<Pickable>());
        return subTabs.size();
    }
    
    public void addTabBack(Node back){
        subTabTG.get(this.currentTabID).addChild(back);
    }
    
    public int addSubItem(Pickable item) {
        item.setHost(this);
        subItems.get(currentTabID).add(item);
        subTabTG.get(currentTabID).addChild((Node)item);
        return subItems.size();
    }
    
    public int addSubItem(int tabID,Pickable item) {
        item.setHost(this);
        subItems.get(currentTabID).add(item);
        subTabTG.get(tabID).addChild((Node)item);
        return subItems.size();
    }
    
    /**
     * 执行子对象发起的事件，不做处理的时间发回控制器处理
     * @param EventName 事件名。每个事件名绑定一个Command对象，对应的Command对象将被执行
     * @param Param 传递给对应Command的参数
     * @return 由对应Command的参数
     */
    public Object doEvent(String eventName, Object Param) {
        if (eventName.equals("SetHotItem")){
            refreshCurrentItem((Pickable)Param);
        }else if(eventName.equals("ChangeTab")){
            currentTab=(ImageButton2D)Param;
            currentTabID=subTabs.indexOf(Param);
            changeTab(currentTabID);
        }
        return myHost.doEvent(eventName, Param);
    }
    
    private void refreshCurrentItem(Pickable currentItem){
        if(this.currentItem.equals(currentItem)) return;    //仅当变化发生时更新
        this.currentItem=currentItem;
        for(int tabID=0; tabID<subTabs.size();tabID++){
           subTabs.get(tabID).setStatue(Pickable.Idle);
           for(int itemID=0; itemID<subItems.get(tabID).size();itemID++)
               subItems.get(tabID).get(itemID).setStatue(Pickable.Idle);
        }
        currentItem.setStatue(Hot);
    }
    
    public void changeTab(int tabID){
        currentTab=subTabs.get(tabID);
        currentTabID=tabID;
        for(int tabCounter=0; tabCounter<subTabs.size();tabCounter++){
            Transform3D t3dTab=new Transform3D();
            if (tabCounter==tabID)
                t3dTab.setTranslation(new Vector3f(0,0,0));
            else
                t3dTab.setTranslation(new Vector3f(-100,-100,0));
            subTabTG.get(tabCounter).setTransform(t3dTab);
        }
        
    }
    /**
     * 处理键盘事件
     * @param evt 待处理的键盘事件
     */
     public void processKeyEvent(KeyEvent evt) {
        if (evt.getID()==KeyEvent.KEY_RELEASED){
            System.err.println(evt.getKeyCode());
            switch(evt.getKeyCode()){
                case KeyEvent.VK_LEFT:
                    //preItem();
                    break;
                case KeyEvent.VK_RIGHT:
                    //nextItem();
                    break;
            }
        }
    }

    /*private void nextItem(){
        int nextItemID=(subItems.indexOf(currentItem)+1)%subItems.size();
        System.err.println(nextItemID);
        refreshCurrentItem(subItems.get(nextItemID));
    }
    private void preItem(){
        int nextItemID=(subItems.indexOf(currentItem)-1);
        if (nextItemID<0) nextItemID+=subItems.size();
        System.err.println(nextItemID);
        refreshCurrentItem(subItems.get(nextItemID));
    }*/
    
    
    /**
     * 为条件调用器调用的方法
     * @param Step 条件调用器送回的步进值
     */
    public void invoke(double Step) {
           for(int itemID=0; itemID<subItems.get(currentTabID).size();itemID++){
               if (subItems.get(currentTabID).get(itemID) instanceof Invokeable){
                   ((Invokeable)subItems.get(currentTabID).get(itemID)).invoke(Step);
               }
           }
    }

    /**
     * 为鼠标拾取行为调用的方法
     * @param evt 拾取当此对象的鼠标事件
     * @deprecated 现在直接由Dock中的对象处理拾取方法
     */
    public void picked(MouseEvent evt) {
    }

    /**
     * 设置此Dock的拥有容器
     * @param host 拥有容器
     */
    public void setHost(IContainer host) {
        myHost=host;
    }
    /**
     * 设置Dock当前在容器中的状态(待命||选中||触发)
     * @param statue Dock当前在容器中的状态
     */
    public void setStatue(int statue) {
        if (currentItem!=null)
            this.currentItem.setStatue(statue);
    }

}

